<?php
// $Id$

/**
 * Controller_Admin_Category 控制器
 */
class Controller_Admin_Category extends Controller_Abstract
{

	function actionIndex()
	{
		// 数据加载
		$this->_view['model'] = Models::find()->getAll();
	}

	function actionAdd()
	{
		$action = url('category/add');
		$modify_id = intval($this->_context->mid);
		$return_array = array('statusCode'=>Helper_Dwz::DWZ_STATUS_ERROR,'message'=>'分类添加失败!');// 服务器响应
		// 当操作为修改
		$modify_obj = false;
		if ($modify_id){
			$action = url('category/add',array('mid'=>$modify_id));
			$modify_obj = Categorys::find('id = ?',$modify_id)->getOne();
			if (!$modify_obj->id())
			{
				return json_encode(array('statusCode' => Helper_Dwz::DWZ_STATUS_ERROR,'message'=>'您想要修改的信息没有找到!'));
			}
		}
		$form = new Form_Category($action);
		if ($this->_context->isPOST())
		{
			// 参数校验
			if ($form->validate($_POST))
			{
				// 逻辑处理
				$form_values = $form->values();
				$file_path = $form_values['imgurl'];
				if ($modify_id && $modify_obj !== false){
					// 修改
					if ($form_values['imgurl'] != '' && $form_values['imgurl'] != $modify_obj->imgurl)
					{
						if (is_file($form_values['imgurl']))
						{
							$new_filepath = str_replace('uploadfiles/tmp', 'uploadfiles/image', $form_values['imgurl']);
							// 转移新的位置去
							if (copy($form_values['imgurl'], $new_filepath)){
								if (is_file($modify_obj->imgurl)){
									unlink($modify_obj->imgurl);// 删除旧的
									unlink($form_values['imgurl']);// 删除tmp
									$form_values['imgurl'] = $new_filepath;
								}
							}
						}else{
							unset($form_values['imgurl']);
						}
					}else{
						unset($form_values['imgurl']);
					}
					// 更改了上级分类
					if ($form_values['parentid'] != $modify_obj->parentid){ 
						$tmp = Categorys::find('id = ?',$form_values['parentid'])->getOne();
						$cate_path = !is_null($tmp->catepath)?$tmp->catepath:'0,';
						$form_values['catepath'] = $cate_path . $modify_obj->id . ',';
						// 更新子分类的catepath
						if ($modify_obj->child)
						{
							foreach ($modify_obj->child as $item)
							{
								Categorys::meta()->updateWhere(array('catepath'=>str_replace($modify_obj->catepath, $form_values['catepath'], $item->catepath),'model'=>$form_values['model']),'id = ?',$item->id);
							}
						}
					}
					$modify_obj->changeProps($form_values);
					$modify_obj->save();
				}else{
					// 添加
					if ($form_values['imgurl'] != '')
					{
						if (is_file($file_path))
						{
							$new_filepath = str_replace('uploadfiles/tmp', 'uploadfiles/image', $form_values['imgurl']);
							// 转移新的位置去
							if (copy($form_values['imgurl'], $new_filepath)){
								if (is_file($new_filepath)){
									$form_values['imgurl'] = $new_filepath;
									unlink($form_values['imgurl']);
								}
							}
						}else{
							$form_values['imgurl'] = '';
						}
					}
					$modify_obj = new Categorys($form_values);
					$modify_obj->save();
					// 写入catepath
					$tmp = Categorys::find('id = ?',$form_values['parentid'])->getOne();
					$cate_path = !is_null($tmp->catepath)?$tmp->catepath:'0,';
					$cate_path .= $modify_obj->id . ',';
					// 修改cate_path
					$modify_obj->changeProps(array('catepath'=>$cate_path));
					$modify_obj->save();
				}
				$return_array = array('statusCode' => Helper_Dwz::DWZ_STATUS_OK,'message'=>'操作已成功!','callbackType'=>'forward','forwardUrl'=>url('admin::category'));
			}else{
				// 参数校验返回
				foreach ($form->elements() as $element){
					if (!$element->isValid()){
						$err = implode(',', $element->errorMsg());
						$return_array['message'] .= $err.'<br>';
					}
				}
			}
			// 返回数据
			return json_encode($return_array);
		}
		// 原始数据导入
		$form->element('model')->items = array_merge(array('0'=>'选择模型'),Models::find()->getAll()->toHashMap('id','mtitle'));
		// 默认数据导入
		if ($modify_obj !== false)
		{
			$form->element('parentid')->items = Categorys::get_category_tree($modify_obj->model);
			$form->import($modify_obj->toArray());
		}
		// 数据加载
		$this->_view['form'] = $form;
	}
	/**
	 *
	 * 分类删除
	 */
	function actionDelete()
	{
		$delete_id = intval($this->_context->did);
		$return_array = array('statusCode'=>Helper_Dwz::DWZ_STATUS_ERROR,'message'=>'分类删除失败!');// 服务器响应
		if ($delete_id) {
			// 删除  含有内容就不能删除
			$category = Categorys::find('id = ?',$delete_id)->getOne();
			if($category->id())
			{
				if (count($category->child))
				{
					$return_array['message'] = '请先删除所有下级分类!';
				}else{
					$content_count = Contents::find('catepath like ?',"%{$category->catepath}%")->getCount();
					if ($content_count){
						$return_array['message'] = '请先删除相关内容后再删除分类!';
					}else{
						// 可以删除
						if ($category->imgurl && is_file($category->imgurl))
						{
							@unlink($category->imgurl);
						}
						Categorys::meta()->deleteWhere('id = ?',$delete_id);
						$return_array = array('statusCode'=>Helper_Dwz::DWZ_STATUS_OK,'message'=>'分类删除成功!','callbackType'=>'forward','forwardUrl'=>url('admin::category'));
					}
				}
			}else{
				$return_array['message'] = '没有找到您要删除的问题!';
			}
			
		}
		return json_encode($return_array);
	}
	/**
	 * 设定详细内容
	 */
	function actionSetcontent()
	{
		$modify_id = intval($this->_context->mid);
		$form = new Form_Setcontent(url('category/setcontent',array('mid'=>$modify_id)));
		$modify_obj = Categorys::find('id = ?',$modify_id)->setColumns('id,ccontent')->getOne();
		if ($modify_obj->id())
		{
			if ($this->_context->isPOST() && $form->validate($_POST))
			{
				$values = $form->values();
				$modify_obj->changeProps(array('ccontent'=>$values['ccontent']));
				$modify_obj->save();
				$return_array = array('statusCode'=>Helper_Dwz::DWZ_STATUS_OK,'message'=>'详细内容设置成功!');
				return json_encode($return_array);
			}
			$form->import($modify_obj->toArray());
		}else{
			$return_array = array('statusCode'=>Helper_Dwz::DWZ_STATUS_ERROR,'message'=>'分类信息不存在!');// 服务器响应
			return json_encode($return_array);
		}
		$this->_view['form'] = $form;
	}
}